package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.CouponUserListVO;
import com.cskaoyan.bean.ListWxData;
import com.cskaoyan.bean.vo.CouponIndexVo;
import com.cskaoyan.service.CouponService;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/coupon")
public class CouponWxController {
    @Autowired
    CouponService couponService;

    @RequestMapping("list")
    public BaseRespVo list(Integer page,Integer size){
        ListWxData<CouponIndexVo> couponIndexVoListWxData = couponService.queryIndexList(page,size);
        return BaseRespVo.ok(couponIndexVoListWxData);
    }

    @RequestMapping("receive")
    public BaseRespVo receive(@RequestBody Map map){
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipal();
        //TODO  补改userId
//        Integer userId = 1;
        Integer couponId = (Integer) map.get("couponId");
        int resCode =  couponService.getCoupon(userId,couponId,null);
        if (resCode==1){
            return BaseRespVo.fail("优惠卷已领完");
        }
        if (resCode==2){
            return BaseRespVo.fail("领取已超过限制");
        }
        return BaseRespVo.ok();
    }


    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map map){
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipal();
        //TODO  补改userId
//        Integer userId = 1;
        String code  = (String) map.get("code");
        int resCode = couponService.getCoupon(userId, null, code);
        if (resCode==3){
            return BaseRespVo.fail("优惠券已兑换");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("mylist")
    public BaseRespVo mylist(Integer status,Integer page,Integer size){
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipal();
        //TODO  补改userId
//        Integer userId = 1;
        ListWxData<CouponUserListVO> couponUserListVO = couponService.selectMyList(userId,status,page,size);
        return BaseRespVo.ok(couponUserListVO);
    }

    @RequestMapping("selectlist")
    public BaseRespVo selectlist(Integer cartId){
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipal();
        //TODO  补改userId
//        Integer userId = 1;
        List<CouponUserListVO> listVOS = couponService.selectCanUseCoupon(cartId,userId);
        return BaseRespVo.ok(listVOS);
    }

}
